import { Controller } from "../../mvc/core/Controller";
import { INpcAwardController } from "./interfaces/INpcAwardController";
import { IView } from "../../mvc/interfaces/IView";
import { NpcAwardModule } from "../../../modules/NpcAwardModule";
import { NpcAwardHintIcon } from "../view/npcAward/NpcAwardHintIcon";
import { NetDispatcher } from "../../mvc/core/NetDispatcher";
import { ServerCommand } from "../mvc/ServerCommand";
import { EPublicCommand } from "../../../Message/Command/EPublicCommand";
import { Dispatcher } from "../../mvc/core/Dispatcher";
import { EventName } from "../mvc/EventName";
import { SActiveToClient } from "../../../Message/Public/SActiveToClient";
import { EActiveType } from "../../../Message/Public/EActiveType";
import { Game } from "../Game";
import { MessageBlock } from "../../../Framework/MQ/MessageBlock";
import { SNpcAward } from "../../../Message/Public/SNpcAward";
import { LoaderHelp } from "../../common/display/LoaderHelp";
import { ResFileConst } from "../resource/ResFileConst";
import { ItemData } from "../resource/info/ItemData";
import { ImageInfo } from "../../../com/gengine/resource/info/ImageInfo";
import { FlyToNavbarTool } from "../view/award/FlyToNavbarTool";
import { INpcAwardModule } from "../../../modules/interfaces/INpcAwardModule";
import { LoaderManager } from "../../../com/gengine/resource/LoaderManager";
import { LoaderPriority } from "../../../com/gengine/resource/LoaderPriority";
import { UIIconEvent } from "../view/uiIconBtn/UIIconEvent";
import { GlobalClass } from "../../../com/mui/core/GlobalClass";
import { UiActiveHintIconView } from "../view/uiIconBtn/UiActiveHintIconView";
import { DataEvent } from "../events/DataEvent";
import { GameProxy } from "../mvc/GameProxy";
import { GameDefConfig } from "../resource/GameDefConfig";
import { Alert } from "../../../com/mui/controls/Alert";
import { Language } from "../../../extend/language/Language";
import { AIManager } from "../scene/ai/AIManager";
import { AIType } from "../scene/ai/AIType";
import { NpcEffectRule } from "../rules/NpcEffectRule";
import { TaskTargetInfo } from "../model/TaskTargetInfo";
import { DefInfo } from "../resource/info/DefInfo";
type int = number;
//class NpcAwardController
    
    export  class NpcAwardController extends Controller implements INpcAwardController
    {
        constructor()
        {
            
            super();this._npcIdArr = [];
            this._npcHintIconArr = [];
            this._activeNpcId = [1000019, 1000020, 1000021, 1000022];
            return;
        }

        protected /* override */ initView(): IView
        {
            if (this._npcAward == null) 
            {
                this._npcAward = new NpcAwardModule();
            }
            return this._npcAward;
        }

        /* internal  */createHintIcon(arg1: int): NpcAwardHintIcon
        {
            var loc1=new NpcAwardHintIcon("NpcAward_" + arg1, false);
            loc1.npcId = arg1;
            return loc1;
        }

        protected /* override */ initServer(): void
        {
            NetDispatcher.addCmdListener(ServerCommand.ActiveNpcAward, this.onActiveNpcAwardHandler);
            NetDispatcher.addCmdListener(ServerCommand.ActiveListChange, this.onActiveNpcAwardHandler);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicNpcAward, this.onPublicNpcAwardHandler);
            NetDispatcher.addCmdListener(ServerCommand.NpcAwardItemUpate, this.onNpcAwardItemUpateHandler);
            Dispatcher.addEventListener(EventName.NpcAwardToGetAward, this.onNpcAwardToGetAwardHandler);
            Dispatcher.addEventListener(EventName.NpcAwardShowTips, this.onNpcAwardShowTipsHandler);
            Dispatcher.addEventListener(EventName.NpcAwardShowModule, this.onNpcAwardShowModuleHandler);
            return;
        }

        /* internal  */onActiveNpcAwardHandler(arg1: SActiveToClient): void
        {
            var loc1=false;
            if (cache.active.isActiveInEffect(EActiveType._EActiveTypeNpcAward)) 
            {
                loc1 = true;
            }
            var loc2=0;
            while (loc2 < this._activeNpcId.length) 
            {
                Game.scene.updateNpcIsAddToStage(this._activeNpcId[loc2], loc1, 400101);
                ++loc2;
            }
            return;
        }

        /* internal  */onPublicNpcAwardHandler(arg1: MessageBlock): void
        {
            var loc3=undefined;
            var loc1=arg1.messageBase as SNpcAward;
            var loc2=loc1.awardMap;
            this._npcIdArr.splice(0);
            var loc4=0;
            var loc5=loc2;
            for (loc3 in loc5) 
            {
                if (!(loc2[loc3] && !(loc2[loc3] == 0))) 
                {
                    continue;
                }
                this._npcIdArr.push(loc3);
                this._npcIdArr.sort();
            }
            LoaderHelp.addResCallBack(ResFileConst.npcAward, this.onResLoaded);
            return;
        }

        /* internal  */onNpcAwardItemUpateHandler(arg1: ItemData): void
        {
            var itemData: ItemData;
            var startPoint: Object /* flash.geom.Point */;
            var imageUrl: string;
            var onLoadCompleteHandler: Function;

            var loc1;
            startPoint = null;
            onLoadCompleteHandler = null;
            itemData = arg1;
            onLoadCompleteHandler = (arg1: ImageInfo): void
            {
                if (arg1) 
                {
                    FlyToNavbarTool.flyToBackPack(arg1.bitmapData, startPoint);
                }
                return;
            }
            startPoint = (view as INpcAwardModule).getModulePoint();
            imageUrl = itemData.itemInfo.item.icon;
            LoaderManager.instance.load(imageUrl + ".jpg", onLoadCompleteHandler, LoaderPriority.LevelB);
            return;
        }

        /* internal  */onResLoaded(): void
        {
            var loc1=null;
            var loc3=0;
            var loc4=0;
            var loc2=0;
            loc2 = 0;
            while (loc2 < this._npcHintIconArr.length) 
            {
                if (this._npcHintIconArr[loc2]) 
                {
                    loc1 = this._npcHintIconArr[loc2];
                    if (loc1.parent) 
                    {
                        loc1.parent.removeChild(loc1);
                        loc1.dispatchEvent(new UIIconEvent(UIIconEvent.RemoveIcon));
                    }
                }
                ++loc2;
            }
            this._npcHintIconArr.splice(0);
            loc2 = 0;
            while (loc2 < this._npcIdArr.length) 
            {
                loc3 = this._npcIdArr[loc2];
                if (GlobalClass.hasRes("NpcAward_" + loc3)) 
                {
                    loc1 = this.createHintIcon(loc3);
                    loc1.show();
                    UiActiveHintIconView.instance.addHintIcon(loc1);
                    this._npcHintIconArr.push(loc1);
                }
                ++loc2;
            }
            if (this._npcAward && !this._npcAward.isHide) 
            {
                if ((loc4 = this._npcIdArr.indexOf(this._defInfo.id)) == -1) 
                {
                    this._npcAward.canGetReward = false;
                }
                else 
                {
                    this._npcAward.canGetReward = true;
                }
            }
            return;
        }

        /* internal  */onNpcAwardToGetAwardHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data as int;
            if (loc1 != 0) 
            {
                GameProxy.roleProxy.getNpcAward(loc1);
            }
            return;
        }

        /* internal  */onNpcAwardShowTipsHandler(arg1: DataEvent): void
        {
            this._npcId = arg1.data as int;
            this._defInfo = GameDefConfig.instance.getNpcAward(this._npcId);
            var loc1="";
            var loc2="";
            if (this._defInfo) 
            {
                this._targetInfo = cache.scene.getNpcByEffect(this._defInfo.value);
                loc1 = this._defInfo.text3;
                loc2 = this._defInfo.text1;
            }
            Alert.okLabel = Language.getString(36400);
            Alert.calcelLabel = Language.getString(36401);
            Alert.showCloseBtn = true;
            Alert.show(loc1, loc2, Alert.OK | Alert.CANCEL, null, this.onAlertCloseHandler);
            return;
        }

        /* internal  */onAlertCloseHandler(arg1: int): void
        {
            if (arg1 != Alert.OK) 
            {
                if (arg1 == Alert.CANCEL) 
                {
                    AIManager.onAIControl(this._targetInfo, AIType.AI_Convey);
                }
            }
            else 
            {
                AIManager.onAutoPathAIControl(this._targetInfo);
            }
            return;
        }

        /* internal  */onNpcAwardShowModuleHandler(arg1: DataEvent): void
        {
            var loc2=0;
            var loc1=arg1.data as int;
            this._defInfo = GameDefConfig.instance.getNpcAwardByEffect(loc1);
            view.show();
            NpcEffectRule.registDialogEffectWindow(view);
            if (this._defInfo) 
            {
                this._npcAward.updateNpcRes(this._defInfo);
                this._npcAward.npcId = this._defInfo.id;
                loc2 = this._npcIdArr.indexOf(this._defInfo.id);
                if (loc2 == -1) 
                {
                    this._npcAward.canGetReward = false;
                }
                else 
                {
                    this._npcAward.canGetReward = true;
                }
            }
            return;
        }

        private /* var */_npcIdArr: Array<any>;

        private /* var */_npcHintIconArr: Array<any>;

        private /* var */_targetInfo: TaskTargetInfo;

        private /* var */_npcAward: INpcAwardModule;

        private /* var */_activeNpcId: Array<any>;

        private /* var */_npcId: int;

        private /* var */_defInfo: DefInfo;
    }
